//
//  APPThemeChangeManage.swift
//  XMenSwift
//
//  Created by Jason on 2023/8/24.
//

import Foundation
import SwiftTheme

@objc extension UIView {
    
    func setThemeBackgroundUIColors(_ uiColors: [UIColor]? = nil) {
        if uiColors == nil {
            self.theme_backgroundColor = AppThemeConfigManage.shared.mainBackgroundColor
        } else {
            self.theme_backgroundColor = ThemeColorPicker.pickerWithUIColors(uiColors!)
        }
    }
    
    func setThemeBackgroundColors(_ colors: [String]? = nil) {
        if colors == nil {
            self.theme_backgroundColor = AppThemeConfigManage.shared.mainBackgroundColor
        } else {
            self.theme_backgroundColor = ThemeColorPicker.pickerWithColors(colors!)
        }
    }
}

@objc extension UILabel {
    
    func setThemeTextUIColors(_ uiColors: [UIColor]? = nil) {
        if uiColors == nil {
            self.theme_textColor = AppThemeConfigManage.shared.mainTextColor
        } else {
            self.theme_textColor = ThemeColorPicker.pickerWithUIColors(uiColors!)
        }
    }
    
    func setThemeTextColors(_ colors: [String]? = nil) {
        if colors == nil {
            self.theme_textColor = AppThemeConfigManage.shared.mainTextColor
        } else {
            self.theme_textColor = ThemeColorPicker.pickerWithColors(colors!)
        }
    }
    
    func setThemeTextFont(style: FontStyle = .main, weight: FontWeightStyle = .regular, size: CGFloat) {
        var tempWeight: UIFont.Weight = .regular
        if weight == .medium {
            tempWeight = .medium
        } else if weight == .bold {
            tempWeight = .bold
        }
        let defaultFont = UIFont.init(name: AppThemeConfigManage.shared.getFont(style: style, weight: weight), size: size) ?? UIFont.systemFont(ofSize: size, weight: tempWeight)
        let otherFont = UIFont.init(name: AppThemeConfigManage.shared.getFont(currentType: .other, style: style, weight: weight), size: size) ?? UIFont.systemFont(ofSize: size, weight: tempWeight)
        self.theme_font = ThemeFontPicker.pickerWithFonts([defaultFont,otherFont])
    }
    
}

@objc extension UIButton {
    
    func setThemeTextFont(style: FontStyle = .main, weight: FontWeightStyle = .regular, size: CGFloat) {
        var tempWeight: UIFont.Weight = .regular
        if weight == .medium {
            tempWeight = .medium
        } else if weight == .bold {
            tempWeight = .bold
        }
        let defaultFont = UIFont.init(name: AppThemeConfigManage.shared.getFont(style: style, weight: weight), size: size) ?? UIFont.systemFont(ofSize: size, weight: tempWeight)
        let otherFont = UIFont.init(name: AppThemeConfigManage.shared.getFont(currentType: .other, style: style, weight: weight), size: size) ?? UIFont.systemFont(ofSize: size, weight: tempWeight)
        self.titleLabel?.theme_font = ThemeFontPicker.pickerWithFonts([defaultFont,otherFont])
    }
    
    func setThemeTextUIColor(_ uiColors: [UIColor]? = nil, forState: UIControl.State) {
        if uiColors == nil {
            self.theme_setTitleColor(AppThemeConfigManage.shared.mainTextColor, forState: forState)
        } else {
            self.theme_setTitleColor(ThemeColorPicker.pickerWithUIColors(uiColors!), forState: forState)
        }
        
    }
    
    func setThemeTextColors(_ colors: [String]? = nil, forState: UIControl.State) {
        if colors == nil {
            self.theme_setTitleColor(AppThemeConfigManage.shared.mainTextColor, forState: forState)
        } else {
            self.theme_setTitleColor(ThemeColorPicker.pickerWithColors(colors!), forState: forState)
        }
    }
    
    func setThemeImageNames(_ names: [String], forState: UIControl.State) {
        self.theme_setImage(ThemeImagePicker.pickerWithNames(names), forState: forState)
    }
    
    func setThemeImages(images: [UIImage], forState: UIControl.State) {
        self.theme_setImage(ThemeImagePicker.pickerWithImages(images), forState: forState)
    }
}

@objc extension UIImageView {
    
    func setThemeImageNames(_ names: [String]) {
        self.theme_image = ThemeImagePicker.pickerWithNames(names)
    }
    
    func setThemeImages(_ images: [UIImage]) {
        self.theme_image = ThemeImagePicker.pickerWithImages(images)
    }
}

@objc class APPThemeChangeManage: NSObject {
    
    //切换主题
    @objc class func setTheme(index: Int) {
        ThemeManager.setTheme(index: index)
    }
    
}
